package com.kkb.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DepartmentMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.Department;
import com.kkb.pojo.DepartmentExample;
import com.kkb.pojo.Hosregister;
import com.kkb.pojo.HosregisterExample;
import com.kkb.vo.HRVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class HosRegisterService {

    @Resource
    private DepartmentMapper departmentMapper;
    /**
     * 将传入进来的科室，转化成对应的科室id
     */
    public Integer queryByDepName(String depName){
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andDepartNameEqualTo(depName);
        List<Department> departments = departmentMapper.selectByExample(example);
        for (Department department : departments) {
            return department.getDepartId();
        }
        return null;
    }

    @Resource
    private  HosregisterMapper hosregisterMapper;

    public PageInfo<Hosregister> queryByPage(Integer pageNum, Integer pageSize, HRVo vo){
        HosregisterExample example = new HosregisterExample();
        HosregisterExample.Criteria criteria = example.createCriteria();
        if (vo!=null){
            if (vo.getHosrId()!=null){ //病历号不等于null
                criteria.andHosrIdEqualTo(vo.getHosrId());
            }
            if (vo.getdId()!=null){   //医生编号
                criteria.andDIdEqualTo(vo.getdId());
            }
            if (vo.getHosrDepartid()!=null && !"".equals(vo.getHosrDepartid())){ //所属科室
                Integer integer = queryByDepName(vo.getHosrDepartid());
                criteria.andHosrDepartidEqualTo(integer);
            }
            if (vo.getBeginDate()!=null){
                criteria.andHosrDateGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andHosrDateLessThanOrEqualTo(vo.getEndDate());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Hosregister> hosregisters = hosregisterMapper.selectByExample(example);
        return new PageInfo<>(hosregisters);
    }
}



